
import './dept.less'
import layer from '@/components/layer'
import permission from '@/components/permission'
export default {
    data() {
        return {
            tableData: {
                filter: {},   // 筛选
                flag: false,
                total: 0,
                sizes: 10,    // 选择每页显示条目个数
                size: 10,
                index: 1,
                data: []
            },
            editLayer: {
                show: false,
                id: 0,
                type: 0,
                title: '新增'
            },
            deleteLayer: {
                show: false,
                id: 0,
                title: '删除',
                tips: '是否确认删除该营业部'
            },
            deptForm: {
                deptName: '',
                cityName: '',
                qyName: '',
                dqName: '',
                fbName: ''
            },
            deptFormRule: {
                deptName: [
                    { required: true, message: '请输入营业部名称', trigger: 'blur' }
                ],
                cityName: [
                    { required: true, message: '请选择城市', trigger: 'change' }
                ],
                qyName: [
                    { required: true, message: '请选择区域', trigger: 'change' }
                ],
                dqName: [
                    { required: true, message: '请选择大区', trigger: 'change' }
                ],
                fbName: [
                    { required: true, message: '请选择所属分部', trigger: 'change' }
                ]
            },
            deptFormRef: 'deptForm',
            deptData: {
                timer: null,
                loading: false,
                cityList: [],
                dqList: [],
                qyList: [],
                fbList: []
            }
        }
    },
    components: {
        layer,
        permission
    },
    mounted() {
        this.$store.state.common.isSystem = false
        this.$store.dispatch('setBreadData', [
            {
                name: '系统管理'
            },
            {
                name: '营业部管理'
            }
        ])
        this.getTableData(1)
    },
    methods: {
        // 筛选
        sumbitFilter() {
            this.tableData.flag = false
            this.getTableData(1)
        },
        // 重置
        resetFilter() {
            this.tableData.filter = {}
            this.sumbitFilter()
        },
        // table每页条数改变
        tableSizeChange(val) {
            this.tableData.size = val
            this.tableData.flag = false
            this.getTableData(1)
        },
        // 获取table数据
        getTableData(index) {
            let _data = this.tableData.filter
            this.tableData.index = index
            _data.pageBean = {}
            _data.pageBean.pageIndex = index
            _data.pageBean.pageSize = this.tableData.size
            api.queryDeptJc(_data)
                .then(res => {
                    if (!this.tableData.flag) {
                        this.tableData.total = res.totalCount
                        this.tableData.flag = true
                    }
                    this.tableData.data = []
                    this.tableData.data = res.list
                })
                .catch(res => {
                    console.log(res)
                })
        },
        // 显示编辑弹窗
        showUpdateLayer(row) {
            this.editLayer.show = true
            this.deptForm = {}
            this.getArea(1)
            this.getArea(2)
            this.getArea(3)
            if (row.id) {
                this.editLayer.type = 1
                this.editLayer.title = '修改'

                let _obj = Object.assign({}, row)
                this.deptForm = _obj
            }
            else {
                this.editLayer.type = 0
                this.editLayer.title = '新增'
            }
        },
        // 搜索城市
        searchCity(val) {
            if (this.deptData.timer) {
                clearTimeout(this.deptData.timer)
            }
            if (val !== '') {
                this.deptData.loading = true
                this.deptData.timer = setTimeout(() => {
                    api.getCity({ name: val })
                        .then(res => {
                            this.deptData.loading = false
                            this.deptData.cityList = res
                        })
                        .catch(err => {
                            console.log(err)
                        })
                }, 200)
            } else {
                this.deptData.cityList = []
            }
        },
        // 编辑提交
        editSubmit() {
            this.$refs[this.deptFormRef].validate((valid) => {
                if (valid) {
                    let _param = {}
                    _param.type = this.editLayer.type
                    _param.data = {}
                    if (this.deptForm.id) {
                        _param.data.id = this.deptForm.id
                    }

                    _param.data.deptName = this.deptForm.deptName
                    _param.data.cityName = this.deptForm.cityName
                    _param.data.qyName = this.deptForm.qyName
                    _param.data.dqName = this.deptForm.dqName
                    _param.data.fbName = this.deptForm.fbName

                    api.editSalesDeptJc(_param)
                        .then(res => {
                            this.$msg.success(this.editLayer.title + '成功')
                            this.editLayer.show = false
                            this.tableData.flag = false
                            this.getTableData(1)
                        })
                        .catch(res => {
                            console.log(res)
                        })
                } else {
                    this.$msg.error('验证失败')
                    return false
                }
            })
        },
        // 显示删除弹窗
        showDeleteLayer(id) {
            this.deleteLayer.show = true
            this.deleteLayer.id = id
        },
        // 删除提交
        deleteSubmit() {
            api.delSalesDeptJc({ id: this.deleteLayer.id })
                .then(res => {
                    this.$msg.success('删除成功')
                    this.deleteLayer.show = false
                    this.sumbitFilter()
                })
                .catch(res => {
                    console.log(res)
                })
        },
        // 查询部门区域（grade：等级 1 分部，2 大区， 3 区域）
        getArea(grade = 1) {
            if (this.deptData.dqList.length == 0) {
                api.getArea({ grade: grade })
                    .then(res => {
                        if (grade == 1) {
                            this.deptData.fbList = res
                        }
                        else if (grade == 2) {
                            this.deptData.dqList = res
                        }
                        else if (grade == 3) {
                            this.deptData.qyList = res
                        }
                    })
                    .catch(err => {
                        console.log(err)
                    })
            }
        }
    }
}